﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKArch.Utils;
using YJKDragCurveJig;

namespace YJKArch.AxisModule
{
	public class RectAxisCreator
	{
		public RectAxisCreator(ExternalCommandData cmdData, UIApplication application, RectAxisDataFromDlg data)
		{
			this.m_cmdData = cmdData;
			this.m_uiApplication = application;
			this.m_document = application.ActiveUIDocument.Document;
			this.m_appCreator = application.Application.Create;
			this.m_docCreator = application.ActiveUIDocument.Document.Create;
			this.m_dataFromDlg = data;
		}

		public Result CreateAxis(ref string message, Autodesk.Revit.DB.Document doc)
		{
			XYZ ptBase = new XYZ(0.0, 0.0, 0.0);
			XYZ xyz = new XYZ(0.0, 0.0, 0.0);
			Transaction transaction = new Transaction(this.m_document, "create");
			transaction.Start();
			CurveArray curveArray = this.CreateXAxisCurves(ptBase);
			CurveArray curveArray2 = this.CreateYAxisCurves(ptBase);
			transaction.Commit();
			List<JigEdgeInfo> list = new List<JigEdgeInfo>();
			foreach (object obj in curveArray)
			{
				JigEdgeInfo item = new JigEdgeInfo((Curve)obj);
				list.Add(item);
			}
			foreach (object obj2 in curveArray2)
			{
				JigEdgeInfo item2 = new JigEdgeInfo((Curve)obj2);
				list.Add(item2);
			}
			if (!DragCurveJigInterface.GetInsertPosition(this.m_cmdData, list, true, ptBase, ref xyz))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			CurveArray curveArray3 = new CurveArray();
			CurveArray curveArray4 = new CurveArray();
			for (int i = 0; i < list.Count; i++)
			{
				Curve curve = list.ElementAt(i).ConvertTo(this.m_uiApplication.Application);
				if (i < curveArray.Size)
				{
					curveArray3.Append(curve);
				}
				else
				{
					curveArray4.Append(curve);
				}
			}
			Transaction transaction2 = new Transaction(this.m_document, "insert");
			transaction2.Start();
			try
			{
				ElementSet elementSet = this.CreateXAxis(curveArray3);
				ElementSet elementSet2 = this.CreateYAxis(curveArray4);
				if (elementSet != null)
				{
					ElementSetIterator elementSetIterator;
					if (this.m_dataFromDlg.XReverse)
					{
						elementSetIterator = elementSet.ReverseIterator();
					}
					else
					{
						elementSetIterator = elementSet.ForwardIterator();
					}
					string text = this.m_dataFromDlg.XStart;
					while (elementSetIterator.MoveNext())
					{
						object obj3 = elementSetIterator.Current;
						Grid grid =  obj3 as Grid;
						GridType gridTypeByName = AssistFunc.GetGridTypeByName(this.m_document, this.m_dataFromDlg.GridType);
						GridType gridType = this.ChangeUpOrDownGridType(gridTypeByName, this.m_dataFromDlg.Up, this.m_dataFromDlg.Down, doc);
						grid.ChangeTypeId(gridType.Id);
						grid.Name = text;
						text = AxisNameManager.GetNextLable(text);
					}
				}
				if (elementSet2 != null)
				{
					ElementSetIterator elementSetIterator2;
					if (this.m_dataFromDlg.YReverse)
					{
						elementSetIterator2 = elementSet2.ReverseIterator();
					}
					else
					{
						elementSetIterator2 = elementSet2.ForwardIterator();
					}
					string text2 = this.m_dataFromDlg.YStart;
					while (elementSetIterator2.MoveNext())
					{
						object obj4 = elementSetIterator2.Current;
						Grid grid2 = obj4 as Grid;
						GridType gridTypeByName2 = AssistFunc.GetGridTypeByName(this.m_document, this.m_dataFromDlg.GridType);
						GridType gridType2 = this.ChangeLeftOrRightGridType(gridTypeByName2, this.m_dataFromDlg.Left, this.m_dataFromDlg.Right, doc);
						grid2.ChangeTypeId(gridType2.Id);
						grid2.Name = text2;
						text2 = AxisNameManager.GetNextLable(text2);
					}
				}
				if (this.m_dataFromDlg.WithDim)
				{
					this.CreateAxisDim(doc, elementSet, false);
					this.CreateAxisDim(doc, elementSet2, true);
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				transaction2.RollBack();
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Exception ex)
			{
				transaction2.RollBack();
				message = ex.Message;
				return Autodesk.Revit.UI.Result.Failed;
			}
			transaction2.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private GridType ChangeLeftOrRightGridType(GridType gt, bool left, bool right, Autodesk.Revit.DB.Document doc)
		{
			SubTransaction subTransaction = new SubTransaction(doc);
			GridType gridType = gt;
			try
			{
				subTransaction.Start();
				if (left && !right)
				{
					gridType = (RectAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_Start", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_Start") as GridType);
						Parameter parameter = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter2 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter.Set(1);
						parameter2.Set(0);
					}
				}
				else if (!left && right)
				{
					gridType = (RectAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_End", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_End") as GridType);
						Parameter parameter3 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter4 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter3.Set(0);
						parameter4.Set(1);
					}
				}
				else if (left && right)
				{
					gridType = (RectAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_Both", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_Both") as GridType);
						Parameter parameter5 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter6 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter5.Set(1);
						parameter6.Set(1);
					}
				}
				else if (!left && !right)
				{
					gridType = (RectAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_None", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_None") as GridType);
						Parameter parameter7 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter8 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter7.Set(0);
						parameter8.Set(0);
					}
				}
				subTransaction.Commit();
			}
			catch
			{
				subTransaction.RollBack();
			}
			return gridType;
		}

		public static Element FindType(Autodesk.Revit.DB.Document doc, Type targetType, string familyName, string typeName, BuiltInCategory catagory)
		{
			IEnumerable<Element> enumerable = ElementFiltering.FindSpecialElements(doc, targetType, catagory);
			Element result = null;
			foreach (Element element in enumerable)
			{
				if (element.Name.Equals(typeName) && element.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_NAME).AsString().Equals(typeName))
				{
					result = element;
					break;
				}
			}
			return result;
		}

		private GridType ChangeUpOrDownGridType(GridType gt, bool up, bool down, Autodesk.Revit.DB.Document doc)
		{
			SubTransaction subTransaction = new SubTransaction(doc);
			GridType gridType = gt;
			try
			{
				subTransaction.Start();
				if (up && !down)
				{
					gridType = (RectAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_Start", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_Start") as GridType);
						Parameter parameter = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter2 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter.Set(1);
						parameter2.Set(0);
					}
				}
				else if (!up && down)
				{
					gridType = (RectAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_End", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_End") as GridType);
						Parameter parameter3 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter4 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter3.Set(0);
						parameter4.Set(1);
					}
				}
				else if (up && down)
				{
					gridType = (RectAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_Both", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_Both") as GridType);
						Parameter parameter5 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter6 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter5.Set(1);
						parameter6.Set(1);
					}
				}
				else if (!up && !down)
				{
					gridType = (RectAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_None", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_None") as GridType);
						Parameter parameter7 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter8 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter7.Set(0);
						parameter8.Set(0);
					}
				}
				gridType.GetParameter(BuiltInParameter.DATUM_BUBBLE_LOCATION_IN_ELEV).Set(3);
				subTransaction.Commit();
			}
			catch
			{
				subTransaction.RollBack();
			}
			return gridType;
		}

		public CurveArray CreateXAxisCurves(XYZ ptBase)
		{
			CurveArray curveArray = new CurveArray();
			if (this.m_dataFromDlg.TopData.Count == 0 && this.m_dataFromDlg.DownData.Count == 0)
			{
				return curveArray;
			}
			RectAxisCreator.m_dExtend = this.m_dataFromDlg.ExtendX;
			double angle = AssistFunc.AngleToRadian(this.m_dataFromDlg.Angle);
			XYZ xyz = new XYZ(ptBase.X, ptBase.Y, 1.0);
			xyz.Normalize();
			this.m_TopDownDistArray = new List<RectDistData>();
			this.SortTopDownData();
			XYZ xyz2 = new XYZ(ptBase.X, ptBase.Y - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Z);
			XYZ xyz3 = new XYZ(ptBase.X, ptBase.Y + AssistFunc.mmToFeet(this.GetAllYDistLen() + RectAxisCreator.m_dExtend), ptBase.Z);
			xyz2 = CurveAssistFunc.Rotate(xyz2, xyz, angle);
			xyz3 = CurveAssistFunc.Rotate(xyz3, xyz, angle);
			Line line = YJKLineEx.YJKGetBound(xyz2, xyz3);
			curveArray.Append(line);
			RectDistData rectDistData = new RectDistData();
			for (int i = 0; i < this.m_TopDownDistArray.Count; i++)
			{
				rectDistData = this.m_TopDownDistArray.ElementAt(i);
				if (rectDistData.Type == 0)
				{
					xyz2 = new XYZ(ptBase.X + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Y, ptBase.Z);
					xyz3 = new XYZ(xyz2.X, ptBase.Y + AssistFunc.mmToFeet(this.GetAllYDistLen() + RectAxisCreator.m_dExtend), xyz2.Z);
				}
				else if (rectDistData.Type == 1)
				{
					xyz2 = new XYZ(ptBase.X + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Y - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Z);
					xyz3 = new XYZ(xyz2.X, ptBase.Y + AssistFunc.mmToFeet(this.GetAllYDistLen()), xyz2.Z);
				}
				else if (rectDistData.Type == 2)
				{
					xyz2 = new XYZ(ptBase.X + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Y - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Z);
					xyz3 = new XYZ(xyz2.X, ptBase.Y + AssistFunc.mmToFeet(this.GetAllYDistLen() + RectAxisCreator.m_dExtend), xyz2.Z);
				}
				xyz2 = CurveAssistFunc.Rotate(xyz2, xyz, angle);
				xyz3 = CurveAssistFunc.Rotate(xyz3, xyz, angle);
				line = YJKLineEx.YJKGetBound(xyz2, xyz3);
				curveArray.Append(line);
			}
			return curveArray;
		}

		public CurveArray CreateXAxisCurvesWithoutExtend(XYZ ptBase)
		{
			CurveArray curveArray = new CurveArray();
			CurveArray result;
			try
			{
				if (this.m_dataFromDlg.TopData.Count == 0 && this.m_dataFromDlg.DownData.Count == 0)
				{
					result = curveArray;
				}
				else
				{
					RectAxisCreator.m_dExtend = this.m_dataFromDlg.ExtendX;
					double angle = AssistFunc.AngleToRadian(this.m_dataFromDlg.Angle);
					XYZ xyz = new XYZ(ptBase.X, ptBase.Y, 1.0);
					xyz.Normalize();
					this.m_TopDownDistArray = new List<RectDistData>();
					this.SortTopDownData();
					XYZ xyz2 = new XYZ(ptBase.X, ptBase.Y - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Z);
					XYZ xyz3 = new XYZ(ptBase.X, ptBase.Y + AssistFunc.mmToFeet(this.GetAllYDistLen() + RectAxisCreator.m_dExtend), ptBase.Z);
					xyz2 = CurveAssistFunc.Rotate(xyz2, xyz, angle);
					xyz3 = CurveAssistFunc.Rotate(xyz3, xyz, angle);
					Line line = YJKLineEx.YJKGetBound(xyz2, xyz3);
					curveArray.Append(line);
					RectDistData rectDistData = new RectDistData();
					for (int i = 0; i < this.m_TopDownDistArray.Count; i++)
					{
						rectDistData = this.m_TopDownDistArray.ElementAt(i);
						if (rectDistData.Type == 0)
						{
							xyz2 = new XYZ(ptBase.X + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Y, ptBase.Z);
							xyz3 = new XYZ(xyz2.X, ptBase.Y + AssistFunc.mmToFeet(this.GetAllYDistLen() + RectAxisCreator.m_dExtend), xyz2.Z);
						}
						else if (rectDistData.Type == 1)
						{
							xyz2 = new XYZ(ptBase.X + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Y - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Z);
							xyz3 = new XYZ(xyz2.X, ptBase.Y + AssistFunc.mmToFeet(this.GetAllYDistLen()), xyz2.Z);
						}
						else if (rectDistData.Type == 2)
						{
							xyz2 = new XYZ(ptBase.X + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Y - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Z);
							xyz3 = new XYZ(xyz2.X, ptBase.Y + AssistFunc.mmToFeet(this.GetAllYDistLen() + RectAxisCreator.m_dExtend), xyz2.Z);
						}
						xyz2 = CurveAssistFunc.Rotate(xyz2, xyz, angle);
						xyz3 = CurveAssistFunc.Rotate(xyz3, xyz, angle);
						line = YJKLineEx.YJKGetBound(xyz2, xyz3);
						curveArray.Append(line);
					}
					result = curveArray;
				}
			}
			catch
			{
				result = curveArray;
			}
			return result;
		}

		private ElementSet CreateXAxis(CurveArray curves)
		{
			string text = AxisNameManager.GetLargestNumber(this.m_document).ToString();
			ElementSet newGrids = this.m_document.GetNewGrids(curves);
			foreach (object obj in newGrids)
			{
				Grid grid =  ((Element)obj) as Grid;
				grid.Name = "HYX-" + text;
				grid.Name = "TempNameX" + text;
				text = AxisNameManager.GetNextLable(text);
				grid.HyExtendToAllLevels();
			}
			return newGrids;
		}

		public CurveArray CreateYAxisCurves(XYZ ptBase)
		{
			CurveArray curveArray = new CurveArray();
			if (this.m_dataFromDlg.LeftData.Count == 0 && this.m_dataFromDlg.RightData.Count == 0)
			{
				return curveArray;
			}
			RectAxisCreator.m_dExtend = this.m_dataFromDlg.ExtendY;
			double angle = AssistFunc.AngleToRadian(this.m_dataFromDlg.Angle);
			XYZ xyz = new XYZ(ptBase.X, ptBase.Y, 1.0);
			xyz.Normalize();
			new AxisListItem();
			this.m_LeftRightDistArray = new List<RectDistData>();
			this.SortLeftRightData();
			XYZ xyz2 = new XYZ(ptBase.X - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Y, ptBase.Z);
			XYZ xyz3 = new XYZ(ptBase.X + AssistFunc.mmToFeet(this.GetAllXDistLen() + RectAxisCreator.m_dExtend), ptBase.Y, ptBase.Z);
			xyz2 = CurveAssistFunc.Rotate(xyz2, xyz, angle);
			xyz3 = CurveAssistFunc.Rotate(xyz3, xyz, angle);
			Line line = YJKLineEx.YJKGetBound(xyz3, xyz2);
			curveArray.Append(line);
			RectDistData rectDistData = new RectDistData();
			for (int i = 0; i < this.m_LeftRightDistArray.Count; i++)
			{
				rectDistData = this.m_LeftRightDistArray.ElementAt(i);
				if (rectDistData.Type == 0)
				{
					xyz2 = new XYZ(ptBase.X - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Y + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Z);
					xyz3 = new XYZ(ptBase.X + AssistFunc.mmToFeet(this.GetAllXDistLen()), xyz2.Y, xyz2.Z);
				}
				else if (rectDistData.Type == 1)
				{
					xyz2 = new XYZ(ptBase.X, ptBase.Y + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Z);
					xyz3 = new XYZ(ptBase.X + AssistFunc.mmToFeet(this.GetAllXDistLen() + RectAxisCreator.m_dExtend), xyz2.Y, xyz2.Z);
				}
				else if (rectDistData.Type == 2)
				{
					xyz2 = new XYZ(ptBase.X - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Y + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Z);
					xyz3 = new XYZ(ptBase.X + AssistFunc.mmToFeet(this.GetAllXDistLen() + RectAxisCreator.m_dExtend), xyz2.Y, xyz2.Z);
				}
				xyz2 = CurveAssistFunc.Rotate(xyz2, xyz, angle);
				xyz3 = CurveAssistFunc.Rotate(xyz3, xyz, angle);
				line = YJKLineEx.YJKGetBound(xyz3, xyz2);
				curveArray.Append(line);
			}
			return curveArray;
		}

		public CurveArray CreateYAxisCurvesWithoutExtend(XYZ ptBase)
		{
			CurveArray curveArray = new CurveArray();
			CurveArray result;
			try
			{
				if (this.m_dataFromDlg.LeftData.Count == 0 && this.m_dataFromDlg.RightData.Count == 0)
				{
					result = curveArray;
				}
				else
				{
					RectAxisCreator.m_dExtend = this.m_dataFromDlg.ExtendY;
					double angle = AssistFunc.AngleToRadian(this.m_dataFromDlg.Angle);
					XYZ xyz = new XYZ(ptBase.X, ptBase.Y, 1.0);
					xyz.Normalize();
					new AxisListItem();
					this.m_LeftRightDistArray = new List<RectDistData>();
					this.SortLeftRightData();
					XYZ xyz2 = new XYZ(ptBase.X - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Y, ptBase.Z);
					XYZ xyz3 = new XYZ(ptBase.X + AssistFunc.mmToFeet(this.GetAllXDistLen() + RectAxisCreator.m_dExtend), ptBase.Y, ptBase.Z);
					xyz2 = CurveAssistFunc.Rotate(xyz2, xyz, angle);
					xyz3 = CurveAssistFunc.Rotate(xyz3, xyz, angle);
					Line line = YJKLineEx.YJKGetBound(xyz3, xyz2);
					curveArray.Append(line);
					RectDistData rectDistData = new RectDistData();
					for (int i = 0; i < this.m_LeftRightDistArray.Count; i++)
					{
						rectDistData = this.m_LeftRightDistArray.ElementAt(i);
						if (rectDistData.Type == 0)
						{
							xyz2 = new XYZ(ptBase.X - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Y + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Z);
							xyz3 = new XYZ(ptBase.X + AssistFunc.mmToFeet(this.GetAllXDistLen()), xyz2.Y, xyz2.Z);
						}
						else if (rectDistData.Type == 1)
						{
							xyz2 = new XYZ(ptBase.X, ptBase.Y + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Z);
							xyz3 = new XYZ(ptBase.X + AssistFunc.mmToFeet(this.GetAllXDistLen() + RectAxisCreator.m_dExtend), xyz2.Y, xyz2.Z);
						}
						else if (rectDistData.Type == 2)
						{
							xyz2 = new XYZ(ptBase.X - AssistFunc.mmToFeet(RectAxisCreator.m_dExtend), ptBase.Y + AssistFunc.mmToFeet(rectDistData.Dist), ptBase.Z);
							xyz3 = new XYZ(ptBase.X + AssistFunc.mmToFeet(this.GetAllXDistLen() + RectAxisCreator.m_dExtend), xyz2.Y, xyz2.Z);
						}
						xyz2 = CurveAssistFunc.Rotate(xyz2, xyz, angle);
						xyz3 = CurveAssistFunc.Rotate(xyz3, xyz, angle);
						line = YJKLineEx.YJKGetBound(xyz3, xyz2);
						curveArray.Append(line);
					}
					result = curveArray;
				}
			}
			catch
			{
				result = curveArray;
			}
			return result;
		}

		private ElementSet CreateYAxis(CurveArray curves)
		{
			string text = AxisNameManager.GetLargestNumber(this.m_document).ToString();
			ElementSet newGrids = this.m_document.GetNewGrids(curves);
			foreach (object obj in newGrids)
			{
				Grid grid =  ((Element)obj) as Grid;
				grid.Name = "HYY-" + text;
				grid.Name = "TempNameY" + text;
				text = AxisNameManager.GetNextLable(text);
				grid.HyExtendToAllLevels();
			}
			return newGrids;
		}

		private void CreateAxisDim(Autodesk.Revit.DB.Document doc, ElementSet arAxis, bool bNeedReversal)
		{
			double val = 1500.0;
			double val2 = 800.0;
			XYZ startPoint = null;
			XYZ endPoint = null;
			XYZ startPoint2 = null;
			XYZ endPoint2 = null;
			XYZ startPoint3 = null;
			XYZ endPoint3 = null;
			XYZ startPoint4 = null;
			XYZ endPoint4 = null;
			ReferenceArray arRefAll = new ReferenceArray();
			ReferenceArray arRefAll2 = new ReferenceArray();
			DimensionType dimensionType = null;
			int num = 0;
			foreach (object obj in arAxis)
			{
				Grid grid =  ((Element)obj) as Grid;
				Line line = grid.Curve as Line;
				if (null != line)
				{
					ReferenceArray refs = new ReferenceArray();
					this.GetLinearGridRef(grid, ref refs);
					XYZ xyz = LineAssistFunc.GetVector(line).Normalize();
					if (num == 0)
					{
						startPoint = line.GetEndPoint(0) + xyz * AssistFunc.mmToFeet(val);
						startPoint2 = line.GetEndPoint(1) - xyz * AssistFunc.mmToFeet(val);
						startPoint3 = line.GetEndPoint(0) + xyz * AssistFunc.mmToFeet(val2);
						startPoint4 = line.GetEndPoint(1) - xyz * AssistFunc.mmToFeet(val2);
						this.AddReference(refs, ref arRefAll2);
					}
					else if (arAxis.Size - 1 == num)
					{
						endPoint = line.GetEndPoint(0) + xyz * AssistFunc.mmToFeet(val);
						endPoint2 = line.GetEndPoint(1) - xyz * AssistFunc.mmToFeet(val);
						endPoint3 = line.GetEndPoint(0) + xyz * AssistFunc.mmToFeet(val2);
						endPoint4 = line.GetEndPoint(1) - xyz * AssistFunc.mmToFeet(val2);
						this.AddReference(refs, ref arRefAll2);
					}
					this.AddReference(refs, ref arRefAll);
					num++;
				}
			}
			DimensionType dimType = this.m_dataFromDlg.DimType;
			if (dimType == null)
			{
				TaskDialog.Show("Error", "没有找到标注样式！");
				return;
			}
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(DimensionType));
			string name = dimType.Name;
			dimensionType = null;
			foreach (Element element in filteredElementCollector)
			{
				if (element.Name.Equals(name))
				{
					dimensionType = (element as DimensionType);
					break;
				}
			}
			if (dimensionType == null)
			{
				dimensionType = (dimType.Duplicate(name) as DimensionType);
			}
			double num2 = dimType.get_Parameter(BuiltInParameter.DIM_WITNS_LINE_EXTENSION_BELOW).AsDouble();
			double num3 = dimType.get_Parameter(BuiltInParameter.WITNS_LINE_EXTENSION).AsDouble();
			Parameter parameter = dimensionType.get_Parameter(BuiltInParameter.DIM_WITNS_LINE_EXTENSION_BELOW);
			if (parameter != null && !parameter.IsReadOnly)
			{
				parameter.Set(num3);
			}
			parameter = dimensionType.get_Parameter(BuiltInParameter.WITNS_LINE_EXTENSION);
			if (parameter != null && !parameter.IsReadOnly)
			{
				parameter.Set(num2);
			}
			try
			{
				Line line2 = YJKLineEx.YJKGetBound(startPoint, endPoint);
				Line line3 = YJKLineEx.YJKGetBound(startPoint2, endPoint2);
				if (bNeedReversal)
				{
					Line line4 = line2;
					line2 = line3;
					line3 = line4;
				}
				this.MakeDimension(line2, line3, num2, num3, arRefAll, dimensionType, dimType);
				Line line5 = YJKLineEx.YJKGetBound(startPoint3, endPoint3);
				Line line6 = YJKLineEx.YJKGetBound(startPoint4, endPoint4);
				if (bNeedReversal)
				{
					Line line7 = line5;
					line5 = line6;
					line6 = line7;
				}
				this.MakeDimension(line5, line6, num2, num3, arRefAll2, dimensionType, dimType);
			}
			catch (Exception)
			{
			}
		}

		private void MakeDimension(Line newLine1, Line newLine2, double dLineExtensionBelow, double dLineExtension, ReferenceArray arRefAll, DimensionType dimTypeNew, DimensionType dimType)
		{
			dimTypeNew = dimType;
			if (dLineExtension < dLineExtensionBelow)
			{
				this.m_docCreator.NewDimension(this.m_document.ActiveView, newLine1, arRefAll, dimType);
				newLine2 = Line.CreateBound(newLine2.GetEndPoint(1), newLine2.GetEndPoint(0));
				this.m_docCreator.NewDimension(this.m_document.ActiveView, newLine2, arRefAll, dimTypeNew);
				return;
			}
			newLine1 = Line.CreateBound(newLine1.GetEndPoint(1), newLine1.GetEndPoint(0));
			this.m_docCreator.NewDimension(this.m_document.ActiveView, newLine1, arRefAll, dimTypeNew);
			this.m_docCreator.NewDimension(this.m_document.ActiveView, newLine2, arRefAll, dimType);
		}

		private void AddReference(ReferenceArray refs, ref ReferenceArray refAry)
		{
			foreach (object obj in refs)
			{
				Reference reference = (Reference)obj;
				refAry.Append(reference);
			}
		}

		private bool GetLinearGridRef(Grid grid, ref ReferenceArray arDimRefs)
		{
			XYZ hyendPoint = grid.Curve.GetEndPoint(0);
			XYZ hyendPoint2 = grid.Curve.GetEndPoint(1);
			foreach (Line line in this.GetElementLines(grid))
			{
				if (line.Reference != null)
				{
					XYZ hyendPoint3 = line.GetEndPoint(0);
					XYZ hyendPoint4 = line.GetEndPoint(1);
					XYZ xyz = null;
					XYZ xyz2 = null;
					Geometry.OverlapType overlapType = Geometry.IsOverlapOfTwoLine(this.To2D(hyendPoint), this.To2D(hyendPoint2), this.To2D(hyendPoint3), this.To2D(hyendPoint4), ref xyz, ref xyz2, true, 1E-09);
					if (overlapType == Geometry.OverlapType._OT_Equality || overlapType == Geometry.OverlapType._OT_FirstIncludeSecond || overlapType == Geometry.OverlapType._OT_SecondIncludeFirst || overlapType == Geometry.OverlapType._OT_Part)
					{
						arDimRefs.Append(line.Reference);
						return true;
					}
				}
			}
			arDimRefs.Append(grid.Curve.Reference);
			return true;
		}

		private XYZ To2D(XYZ pt)
		{
			return new XYZ(pt.X, pt.Y, 0.0);
		}

		private List<Line> GetElementLines(Element e)
		{
			List<Line> list = new List<Line>();
			Options options = new Options();
			options.ComputeReferences = true;
			options.IncludeNonVisibleObjects = false;
			options.View = this.m_document.ActiveView;
			List<Line> result;
			try
			{
				GeometryElement geometryElement = e.get_Geometry(options);
				if (geometryElement == null)
				{
					result = list;
				}
				else
				{
					List<GeometryObject> geometryObjectArray = RevitVersionFuncs.GetGeometryObjectArray(geometryElement);
					for (int i = 0; i < geometryObjectArray.Count; i++)
					{
						GeometryObject geometryObject = geometryObjectArray[i];
						if (geometryObject is Line)
						{
							Line item = geometryObject as Line;
							list.Add(item);
						}
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = list;
			}
			return result;
		}

		public CurveArray GetDimCurve(CurveArray gridCurve)
		{
			CurveArray curveArray = new CurveArray();
			CurveArray result;
			try
			{
				double val = 1000.0;
				double val2 = 600.0;
				XYZ startPoint = null;
				XYZ endPoint = null;
				XYZ startPoint2 = null;
				XYZ endPoint2 = null;
				XYZ xyz = null;
				XYZ xyz2 = null;
				XYZ xyz3 = null;
				XYZ xyz4 = null;
				int num = 0;
				foreach (object obj in gridCurve)
				{
					Line line = ((Curve)obj) as Line;
					if (null != line)
					{
						XYZ xyz5 = LineAssistFunc.GetVector(line).Normalize();
						XYZ xyz6 = Geometry.RotateTo(xyz5, -45.0, XYZ.BasisZ);
						if (num == 0)
						{
							startPoint = line.GetEndPoint(0) + xyz5 * AssistFunc.mmToFeet(val);
							startPoint2 = line.GetEndPoint(1) - xyz5 * AssistFunc.mmToFeet(val);
							xyz = line.GetEndPoint(0) - xyz5 * AssistFunc.mmToFeet(val2);
							xyz3 = line.GetEndPoint(1) + xyz5 * AssistFunc.mmToFeet(val2);
							Line line2 = YJKLineEx.YJKGetBound(xyz - xyz5 * AssistFunc.mmToFeet(300.0), xyz + xyz5 * AssistFunc.mmToFeet(300.0));
							Line line3 = YJKLineEx.YJKGetBound(xyz3 - xyz5 * AssistFunc.mmToFeet(300.0), xyz3 + xyz5 * AssistFunc.mmToFeet(300.0));
							Line line4 = YJKLineEx.YJKGetBound(xyz - xyz6 * AssistFunc.mmToFeet(300.0), xyz + xyz6 * AssistFunc.mmToFeet(300.0));
							Line line5 = YJKLineEx.YJKGetBound(xyz3 - xyz6 * AssistFunc.mmToFeet(300.0), xyz3 + xyz6 * AssistFunc.mmToFeet(300.0));
							curveArray.Append(line2);
							curveArray.Append(line3);
							curveArray.Append(line4);
							curveArray.Append(line5);
						}
						else if (gridCurve.Size - 1 == num)
						{
							endPoint = line.GetEndPoint(0) + xyz5 * AssistFunc.mmToFeet(val);
							endPoint2 = line.GetEndPoint(1) - xyz5 * AssistFunc.mmToFeet(val);
							xyz2 = line.GetEndPoint(0) - xyz5 * AssistFunc.mmToFeet(val2);
							xyz4 = line.GetEndPoint(1) + xyz5 * AssistFunc.mmToFeet(val2);
							Line line6 = YJKLineEx.YJKGetBound(xyz2 - xyz5 * AssistFunc.mmToFeet(300.0), xyz2 + xyz5 * AssistFunc.mmToFeet(300.0));
							Line line7 = YJKLineEx.YJKGetBound(xyz4 - xyz5 * AssistFunc.mmToFeet(300.0), xyz4 + xyz5 * AssistFunc.mmToFeet(300.0));
							Line line8 = YJKLineEx.YJKGetBound(xyz2 - xyz6 * AssistFunc.mmToFeet(300.0), xyz2 + xyz6 * AssistFunc.mmToFeet(300.0));
							Line line9 = YJKLineEx.YJKGetBound(xyz4 - xyz6 * AssistFunc.mmToFeet(300.0), xyz4 + xyz6 * AssistFunc.mmToFeet(300.0));
							curveArray.Append(line6);
							curveArray.Append(line7);
							curveArray.Append(line8);
							curveArray.Append(line9);
						}
						num++;
					}
				}
				YJKLineEx.YJKGetBound(startPoint, endPoint);
				YJKLineEx.YJKGetBound(startPoint2, endPoint2);
				Line line10 = YJKLineEx.YJKGetBound(xyz, xyz2);
				Line line11 = YJKLineEx.YJKGetBound(xyz3, xyz4);
				curveArray.Append(line10);
				curveArray.Append(line11);
				result = curveArray;
			}
			catch
			{
				result = curveArray;
			}
			return result;
		}

		private double GetAllXDistLen()
		{
			double num = 0.0;
			double num2 = 0.0;
			AxisListItem axisListItem = new AxisListItem();
			for (int i = 0; i < this.m_dataFromDlg.TopData.Count; i++)
			{
				axisListItem = this.m_dataFromDlg.TopData.ElementAt(i);
				int number = axisListItem.Number;
				for (int j = 0; j < number; j++)
				{
					num += axisListItem.Dist;
				}
			}
			for (int k = 0; k < this.m_dataFromDlg.DownData.Count; k++)
			{
				axisListItem = this.m_dataFromDlg.DownData.ElementAt(k);
				int number2 = axisListItem.Number;
				for (int l = 0; l < number2; l++)
				{
					num2 += axisListItem.Dist;
				}
			}
			double result;
			if (num > num2)
			{
				result = num;
			}
			else
			{
				result = num2;
			}
			return result;
		}

		private double GetAllYDistLen()
		{
			double num = 0.0;
			double num2 = 0.0;
			AxisListItem axisListItem = new AxisListItem();
			for (int i = 0; i < this.m_dataFromDlg.LeftData.Count; i++)
			{
				axisListItem = this.m_dataFromDlg.LeftData.ElementAt(i);
				int number = axisListItem.Number;
				for (int j = 0; j < number; j++)
				{
					num += axisListItem.Dist;
				}
			}
			for (int k = 0; k < this.m_dataFromDlg.RightData.Count; k++)
			{
				axisListItem = this.m_dataFromDlg.RightData.ElementAt(k);
				int number2 = axisListItem.Number;
				for (int l = 0; l < number2; l++)
				{
					num2 += axisListItem.Dist;
				}
			}
			double result;
			if (num > num2)
			{
				result = num;
			}
			else
			{
				result = num2;
			}
			return result;
		}

		private void SortLeftRightData()
		{
			this.m_LeftRightDistArray.Clear();
			AxisListItem axisListItem = new AxisListItem();
			double num = 0.0;
			for (int i = 0; i < this.m_dataFromDlg.LeftData.Count; i++)
			{
				axisListItem = this.m_dataFromDlg.LeftData.ElementAt(i);
				for (int j = 0; j < axisListItem.Number; j++)
				{
					if (axisListItem.Dist > 0.0)
					{
						num += axisListItem.Dist;
						RectDistData rectDistData = new RectDistData();
						rectDistData.Type = 0;
						rectDistData.Dist = num;
						this.m_LeftRightDistArray.Add(rectDistData);
					}
				}
			}
			double num2 = 0.0;
			for (int k = 0; k < this.m_dataFromDlg.RightData.Count; k++)
			{
				axisListItem = this.m_dataFromDlg.RightData.ElementAt(k);
				for (int l = 0; l < axisListItem.Number; l++)
				{
					num2 += axisListItem.Dist;
					RectDistData rectDistData2 = new RectDistData();
					rectDistData2.Type = 1;
					rectDistData2.Dist = num2;
					if (this.needAddLeftRightDist(num2, rectDistData2))
					{
						this.m_LeftRightDistArray.Add(rectDistData2);
					}
				}
			}
			if (this.m_LeftRightDistArray.Count <= 0)
			{
				return;
			}
			this.m_LeftRightDistArray.Last<RectDistData>().Type = 2;
			if (this.m_dataFromDlg.LeftData.Count == 0 || this.m_dataFromDlg.RightData.Count == 0)
			{
				for (int m = 0; m < this.m_LeftRightDistArray.Count; m++)
				{
					this.m_LeftRightDistArray.ElementAt(m).Type = 2;
				}
			}
		}

		private bool needAddLeftRightDist(double dDist, RectDistData distData)
		{
			for (int i = 0; i < this.m_LeftRightDistArray.Count; i++)
			{
				double dist = this.m_LeftRightDistArray.ElementAt(i).Dist;
				if (dist.Equals(dDist))
				{
					this.m_LeftRightDistArray.ElementAt(i).Type = 2;
					return false;
				}
				if (dist > dDist)
				{
					this.m_LeftRightDistArray.Insert(i, distData);
					return false;
				}
			}
			return true;
		}

		private void SortTopDownData()
		{
			this.m_TopDownDistArray.Clear();
			AxisListItem axisListItem = new AxisListItem();
			double num = 0.0;
			for (int i = 0; i < this.m_dataFromDlg.TopData.Count; i++)
			{
				axisListItem = this.m_dataFromDlg.TopData.ElementAt(i);
				for (int j = 0; j < axisListItem.Number; j++)
				{
					if (axisListItem.Dist > 0.0)
					{
						num += axisListItem.Dist;
						RectDistData rectDistData = new RectDistData();
						rectDistData.Type = 0;
						rectDistData.Dist = num;
						this.m_TopDownDistArray.Add(rectDistData);
					}
				}
			}
			double num2 = 0.0;
			for (int k = 0; k < this.m_dataFromDlg.DownData.Count; k++)
			{
				axisListItem = this.m_dataFromDlg.DownData.ElementAt(k);
				for (int l = 0; l < axisListItem.Number; l++)
				{
					num2 += axisListItem.Dist;
					RectDistData rectDistData2 = new RectDistData();
					rectDistData2.Type = 1;
					rectDistData2.Dist = num2;
					if (this.needAddTopDownDist(num2, rectDistData2))
					{
						this.m_TopDownDistArray.Add(rectDistData2);
					}
				}
			}
			if (this.m_TopDownDistArray.Count <= 0)
			{
				return;
			}
			this.m_TopDownDistArray.Last<RectDistData>().Type = 2;
			if (this.m_dataFromDlg.TopData.Count == 0 || this.m_dataFromDlg.DownData.Count == 0)
			{
				for (int m = 0; m < this.m_TopDownDistArray.Count; m++)
				{
					this.m_TopDownDistArray.ElementAt(m).Type = 2;
				}
			}
		}

		private bool needAddTopDownDist(double dDist, RectDistData distData)
		{
			for (int i = 0; i < this.m_TopDownDistArray.Count; i++)
			{
				double dist = this.m_TopDownDistArray.ElementAt(i).Dist;
				if (dist.Equals(dDist))
				{
					this.m_TopDownDistArray.ElementAt(i).Type = 2;
					return false;
				}
				if (dist > dDist)
				{
					this.m_TopDownDistArray.Insert(i, distData);
					return false;
				}
			}
			return true;
		}

		private ExternalCommandData m_cmdData;

		private UIApplication m_uiApplication;

		private Autodesk.Revit.DB.Document m_document;

        private Autodesk.Revit.Creation.Application m_appCreator;

        private Autodesk.Revit.Creation.Document m_docCreator;

		private RectAxisDataFromDlg m_dataFromDlg;

		private static double m_dExtend = 3000.0;

		private List<RectDistData> m_TopDownDistArray;

		private List<RectDistData> m_LeftRightDistArray;
	}
}
